package com.yanqu.road.dao.impl.cross.unionwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.unionwar.UnionWarDispatchDetail;
import com.yanqu.road.entity.activity.unionwar.UnionWarUserPatrons;
import com.yanqu.road.entity.activity.unionwar.UnionWarUserSilver;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionWarDispatchDetailDaoImpl extends TempDataObjectDao<UnionWarDispatchDetail> {

    public static String getUnionWarDispatchDetailSql = "select * from t_u_activity_unionwar_dispatch_detail where activityId=? ";
    public Map<String, Map<Integer, Map<Integer, UnionWarDispatchDetail>>> getUnionWarDispatchDetail(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<Integer, Map<Integer, UnionWarDispatchDetail>>> resultMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUnionWarDispatchDetailSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionWarDispatchDetail data = parse(rs);
                if(!resultMap.containsKey(data.getUnionUid())){
                    resultMap.put(data.getUnionUid(), new ConcurrentHashMap<>());
                }
                if(!resultMap.get(data.getUnionUid()).containsKey(data.getPhase())){
                    resultMap.get(data.getUnionUid()).put(data.getPhase(), new ConcurrentHashMap<>());
                }
                if(!resultMap.get(data.getUnionUid()).get(data.getPhase()).containsKey(data.getSession())){
                    resultMap.get(data.getUnionUid()).get(data.getPhase()).put(data.getSession(), data);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }


    public static String insertUnionWarDispatchDetailSql = "insert into t_u_activity_unionwar_dispatch_detail(activityId,unionUid,unionInfo,phase,session,patronsMap,silverMap,userBaseInfoMap,enemyUnionUid,enemyUnionInfo,lastUpdateTime,lastPushTime,winMark,readUserId,groupId) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UnionWarDispatchDetail data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getUnionInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPhase()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSession()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getPatronsMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getSilverMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getUserBaseInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getEnemyUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getEnemyUnionInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastPushTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWinMark()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getReadUserId(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertUnionWarDispatchDetailSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUnionWarDispatchDetailSql = "update t_u_activity_unionwar_dispatch_detail set unionInfo=?,patronsMap=?,silverMap=?,userBaseInfoMap=?,enemyUnionUid=?,enemyUnionInfo=?,lastUpdateTime=?,lastPushTime=?,winMark=?,readUserId=?,groupId=? where activityId=? and unionUid=? and phase=? and session=?";

    @Override
    protected boolean updateData(UnionWarDispatchDetail data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getUnionInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getPatronsMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getSilverMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getUserBaseInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getEnemyUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getEnemyUnionInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastPushTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWinMark()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getReadUserId(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPhase()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSession()));
        if (getDb().executeNoneQuery(updateUnionWarDispatchDetailSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionWarDispatchDetail parseData(ResultSet rs) throws SQLException {
        UnionWarDispatchDetail data = new UnionWarDispatchDetail();
        data.setActivityId(rs.getInt("activityId"));
        data.setUnionUid(rs.getString("unionUid"));
        String unionInfoStr = rs.getString("unionInfo");
        if (!StringUtils.isNullOrEmpty(unionInfoStr)) {
            data.setUnionInfo(JSON.parseObject(unionInfoStr, new TypeReference<UnionBaseInfo>() {
            }));
        }
        data.setPhase(rs.getInt("phase"));
        data.setSession(rs.getInt("session"));

        String patronsMapStr = rs.getString("patronsMap");
        if (!StringUtils.isNullOrEmpty(patronsMapStr)) {
            data.setPatronsMap(JSON.parseObject(patronsMapStr, new TypeReference<ConcurrentHashMap<Long, UnionWarUserPatrons>>() {
            }));
        }

        String silverMapStr = rs.getString("silverMap");
        if (!StringUtils.isNullOrEmpty(silverMapStr)) {
            data.setSilverMap(JSON.parseObject(silverMapStr, new TypeReference<ConcurrentHashMap<Long, UnionWarUserSilver>>() {
            }));
        }

        String userBaseInfoMapStr = rs.getString("userBaseInfoMap");
        if (!StringUtils.isNullOrEmpty(userBaseInfoMapStr)) {
            data.setUserBaseInfoMap(JSON.parseObject(userBaseInfoMapStr, new TypeReference<ConcurrentHashMap<Long, UserBaseInfo>>() {
            }));
        }
        data.setEnemyUnionUid(rs.getString("enemyUnionUid"));
        String enemyUnionInfoStr = rs.getString("enemyUnionInfo");
        if (!StringUtils.isNullOrEmpty(enemyUnionInfoStr)) {
            data.setEnemyUnionInfo(JSON.parseObject(enemyUnionInfoStr, new TypeReference<UnionBaseInfo>() {
            }));
        }
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        data.setLastPushTime(rs.getLong("lastPushTime"));
        data.setWinMark(rs.getInt("winMark"));
        data.setReadUserId(StringUtils.stringToLongList(rs.getString("readUserId"), ","));
        data.setGroupId(rs.getInt("groupId"));
        return data;
    }
}
